//The MIT License
//
//Copyright (c) 2009 nodchip
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.
package tv.dyndns.kishibe.client.game;

import tv.dyndns.kishibe.client.Constant;
import tv.dyndns.kishibe.client.Controller;
import tv.dyndns.kishibe.client.Service;
import tv.dyndns.kishibe.client.SharedData;
import tv.dyndns.kishibe.client.SoundPlayer;
import tv.dyndns.kishibe.client.UserData;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.VerticalPanel;

public abstract class WidgetInputBase extends VerticalPanel {
	private static final String[][] LETTERS = { { "1", "2", "3", "4" }, { "A", "B", "C", "D" } };
	protected static final String ANSWERED = "(解答済)";
	protected static final int DIGIT = 0;
	protected static final int ALPHA = 1;
	protected WidgetTimer timer;
	private PanelQuestionBase panelQuestionBase;

	protected WidgetInputBase() {
		setVerticalAlignment(ALIGN_TOP);
		setHorizontalAlignment(ALIGN_CENTER);
	}

	public abstract void enable(boolean b);

	public abstract void hideAnswer();

	protected void sendAnswer(final String answer) {
		playSound(Constant.SOUND_URL_BUTTON_OK);
		enable(false);

		final int sessionId = SharedData.getInstance().getSessionId();
		final int playerListId = SharedData.getInstance().getPlayerListIndex();

		if (panelQuestionBase != null) {
			panelQuestionBase.displayRatio();
		}

		Service.Util.getInstance().sendAnswer(sessionId, playerListId, answer, callback);

		if (UserData.getInstance().isHideAnswer()) {
			hideAnswer();
		}
	}

	private final AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>() {
		public void onSuccess(Boolean result) {
			SceneGame scene = (SceneGame) Controller.getInstance().getScene();
			scene.onSendAnswer();
		}

		public void onFailure(Throwable caught) {
			Controller.getInstance().showErrorMessage("WidgetInputBase.sendAnswer() - 解答の送信に失敗しました。パケットを再送します。");
			Controller.getInstance().showErrorMessage("WidgetInputBase.sendAnswer() - " + caught.getMessage());

			// sendAnswer(answer);
		}
	};

	protected void playSound(String url) {
		SoundPlayer.getInstance().play(url);
	}

	public void setPanelQuestion(PanelQuestionBase panelQuestionBase) {
		this.panelQuestionBase = panelQuestionBase;
	}

	public String getAnswerMark(String answer) {
		return answer;
	}

	public String getChoiceMark(String choice) {
		return choice;
	}

	protected String getLetter(int letterType, int letterIndex) {
		return LETTERS[letterType][letterIndex];
	}
}
